package com.ficus.road.maintain.grpc;

import com.ficus.road.maintain.config.DiseaseConfig;
import com.ficus.road.maintain.config.DiseaseConfigInfo;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.GpsResult;
import com.ficus.road.maintain.core.model.Point;
import com.ficus.road.maintain.core.model.RelPoint;
import com.ficus.road.maintain.core.model.digitalTwin.*;
import com.google.common.collect.Lists;
import ficus.car_demo.FrameMeta;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
public class RequestConverter {
    public static List<String> getElementTypes() {
        return Lists.newArrayList("PIC_FENCE_RESULT", "PIC_TRAFFIC_SIGN_RESULT", "PIC_MANHOLE_COVER_RESULT",
                "PIC_TRAFFIC_LIGHT_RESULT", "PIC_CURBSTONE_RESULT", "PIC_MILESTONE_RESULT", "PIC_DUSTBIN_RESULT",
                "PIC_RED_AND_WHITE_ROD_RESULT", "PIC_BOLLARD_RESULT", "PIC_ISOLATION_PIER_RESULT", "PIC_SPEED_BUMP_RESULT",
                "PIC_DRAIN_RESULT", "PIC_EXPANSION_JOINT_RESULT", "PIC_GUTTER_INLET_RESULT", "PIC_BUS_STATION_RESULT");
    }

    public static List<Asset> frame2Assets(FrameMeta.FrameResult frameResult) {
        List<Asset> assets = Lists.newArrayList();
        try {
            long timestamp = frameResult.getTimeStamp() / 1000;
            //生成历史告警
            Frame frame = new Frame();
            frame.setCreationTime(new Date());
            frame.setTimestamp(timestamp);
            com.ficus.road.maintain.core.model.digitalTwin.GpsResult gpsResult = com.ficus.road.maintain.core.model.digitalTwin.GpsResult.builder()
                    .latitude(frameResult.getGpsResult().getLatitude())
                    .longitude(frameResult.getGpsResult().getLongitude())
                    .build();
            frame.setLocation(gpsResult);
//            System.out.println(frameResult.getTimeStamp());

            frameResult.getPerceptionResultSet().getRoadPerceptionResultListList().forEach(roadPerceptionResult -> {

                FrameMeta.RoadPerceptionResult.ResultCase resultCase = roadPerceptionResult.getResultCase();
                Asset asset = new Asset();
                asset.setCode(resultCase.getNumber());
                asset.setTimestamp(timestamp);
                asset.setCreationTime(new Date());
                asset.setGpsResult(gpsResult);
                asset.setLocation(Lists.newArrayList(gpsResult.getLongitude(), gpsResult.getLatitude()));
                asset.setType(resultCase.toString());
                if (!getElementTypes().contains(asset.getType())) {
                    return;
                }

                Object result_ = getFieldValueByFieldName("result_", roadPerceptionResult);
                String trackId = resultCase + "_" + getFieldValueByFieldName("trackId_", result_);

                FrameMeta.Bbox bbox = (FrameMeta.Bbox) getFieldValueByFieldName("bbox_", result_);
                asset.setBBox(BBox.builder().x(bbox.getX()).y(bbox.getY()).w(bbox.getW()).h(bbox.getH()).build());

                FrameMeta.RelBbox relBbox = (FrameMeta.RelBbox) getFieldValueByFieldName("relBbox_", result_);
                asset.setRelBBox(RelBBox.builder().x(relBbox.getX()).y(relBbox.getY()).w(relBbox.getW()).h(relBbox.getH()).build());

                FrameMeta.Point point = (FrameMeta.Point) getFieldValueByFieldName("point_", result_);
                asset.setPoint(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(point.getX()).y(point.getY()).build());

                FrameMeta.RelPoint relPoint = (FrameMeta.RelPoint) getFieldValueByFieldName("relPoint_", result_);
                asset.setRelPoint(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relPoint.getX()).y(relPoint.getY()).build());

                FrameMeta.Cube cube = (FrameMeta.Cube) getFieldValueByFieldName("cube_", result_);
                if (Objects.nonNull(cube)) {
                    asset.setCube(Cube.builder()
                            .p1(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP1().getX()).y(cube.getP1().getY()).build())
                            .p2(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP2().getX()).y(cube.getP2().getY()).build())
                            .p3(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP3().getX()).y(cube.getP3().getY()).build())
                            .p4(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP4().getX()).y(cube.getP4().getY()).build())
                            .p5(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP5().getX()).y(cube.getP5().getY()).build())
                            .p6(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP6().getX()).y(cube.getP6().getY()).build())
                            .p7(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP7().getX()).y(cube.getP7().getY()).build())
                            .p8(com.ficus.road.maintain.core.model.digitalTwin.Point.builder().x(cube.getP8().getX()).y(cube.getP8().getY()).build())
                            .build());
                }

                FrameMeta.RelCube relCube = (FrameMeta.RelCube) getFieldValueByFieldName("relCube_", result_);

                if (Objects.nonNull(relCube)) {
                    asset.setRelCube(RelCube.builder()
                            .p1(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP1().getX()).y(relCube.getRelP1().getY()).build())
                            .p2(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP2().getX()).y(relCube.getRelP2().getY()).build())
                            .p3(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP3().getX()).y(relCube.getRelP3().getY()).build())
                            .p4(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP4().getX()).y(relCube.getRelP4().getY()).build())
                            .p5(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP5().getX()).y(relCube.getRelP5().getY()).build())
                            .p6(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP6().getX()).y(relCube.getRelP6().getY()).build())
                            .p7(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP7().getX()).y(relCube.getRelP7().getY()).build())
                            .p8(com.ficus.road.maintain.core.model.digitalTwin.RelPoint.builder().x(relCube.getRelP8().getX()).y(relCube.getRelP8().getY()).build())
                            .build());
                }

                asset.setCreationTime(new Date());
                asset.setTrackId(trackId);
                asset.setCreationTime(new Date());
                assets.add(asset);
            });
        } catch (Exception e) {
            log.error("exception ", e);
        }

        return assets;
    }

    /**
     * 根据属性名获取属性值
     */
    private static Object getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            field.setAccessible(true);
            return field.get(object);
        } catch (Exception e) {
            return null;
        }
    }

    public static FrameDTO request2FrameDTO(FrameMeta.FrameResult request, DiseaseConfig diseaseConfig) {
        List<FrameMeta.RoadDiseaseResult> resultList = request.getDiseaseResultListList();
        List<RoadDiseaseResult> diseaseResults = new ArrayList<>();
        resultList.forEach(t -> {
            FrameMeta.RoadDiseaseResult.ResultCase resultCase = t.getResultCase();

            String type;
            String roadType = null;
            int width = 0;
            float length = 0;
            float area = 0;
            float angle = 0;
            int heightDiff = 0;
            int fallDownNumber = 0;
            Integer headCount = null;
            Float comfortRate = null;
            Float score = null;
            String subType = null;
            FrameMeta.Bbox bbox1 = null;
            FrameMeta.RelBbox relBbox1 = null;
            FrameMeta.SeverityType severityType = null;


            type = resultCase.toString();
            FrameMeta.RepairStateType repairState = null;
            boolean needPushOut = false;
            switch (resultCase) {
                case PIC_CRACK_RESULT:
                    bbox1 = t.getPicCrackResult().getBbox();
                    relBbox1 = t.getPicCrackResult().getRelBbox();
                    width = (int) t.getPicCrackResult().getWidth();
                    heightDiff = (int) t.getPicCrackResult().getHeightDiff();
                    length = t.getPicCrackResult().getLength();
                    area = t.getPicCrackResult().getArea();
                    severityType = t.getPicCrackResult().getSeverity();
                    score = t.getPicCrackResult().getScore();
                    roadType = t.getPicCrackResult().getRoadType().toString();
                    FrameMeta.PicCrackType crackType = t.getPicCrackResult().getCrackType();
                    needPushOut = t.getPicCrackResult().getNeedPushOut();
                    repairState = t.getPicCrackResult().getRepairState();
                    subType = crackType.toString();
                    break;
                case PIC_POTHOLES_RESULT:
                    bbox1 = t.getPicPotholesResult().getBbox();
                    relBbox1 = t.getPicPotholesResult().getRelBbox();
                    width = (int) t.getPicPotholesResult().getWidth();
                    severityType = t.getPicPotholesResult().getSeverity();
                    score = t.getPicPotholesResult().getScore();
                    length = t.getPicPotholesResult().getLength();
                    area = t.getPicPotholesResult().getArea();
                    heightDiff = (int) t.getPicPotholesResult().getHeightDiff();
                    roadType = t.getPicPotholesResult().getRoadType().toString();
                    needPushOut = t.getPicPotholesResult().getNeedPushOut();
                    FrameMeta.PicCrackType crack = t.getPicPotholesResult().getCrackType();
                    subType = crack.toString();
                    repairState = t.getPicPotholesResult().getRepairState();
                    break;
                case PIC_MANHOLE_COVER_DISLOCATION_RESULT:
                    bbox1 = t.getPicManholeCoverDislocationResult().getBbox();
                    relBbox1 = t.getPicManholeCoverDislocationResult().getRelBbox();
                    score = t.getPicManholeCoverDislocationResult().getScore();
                    heightDiff = (int) t.getPicManholeCoverDislocationResult().getHeightDiff();
                    needPushOut = t.getPicManholeCoverDislocationResult().getNeedPushOut();
                    subType = t.getPicManholeCoverDislocationResult().getDiseaseType().toString();
                    repairState = t.getPicManholeCoverDislocationResult().getRepairState();
                    break;
                case PIC_FENCE_DISEASE_RESULT:
                    bbox1 = t.getPicFenceDiseaseResult().getBbox();
                    relBbox1 = t.getPicFenceDiseaseResult().getRelBbox();
                    score = t.getPicFenceDiseaseResult().getScore();
                    angle = t.getPicFenceDiseaseResult().getAngle();
                    fallDownNumber = t.getPicFenceDiseaseResult().getFallDownNumber();
                    needPushOut = t.getPicFenceDiseaseResult().getNeedPushOut();
                    subType = t.getPicFenceDiseaseResult().getDiseaseType().toString();
                    repairState = t.getPicFenceDiseaseResult().getRepairState();
                    break;
                case PIC_RED_AND_WHITE_ROD_DISEASE_RESULT:
                    bbox1 = t.getPicRedAndWhiteRodDiseaseResult().getBbox();
                    relBbox1 = t.getPicRedAndWhiteRodDiseaseResult().getRelBbox();
                    score = t.getPicRedAndWhiteRodDiseaseResult().getScore();
                    angle = t.getPicRedAndWhiteRodDiseaseResult().getAngle();
                    fallDownNumber = t.getPicRedAndWhiteRodDiseaseResult().getFallDownNumber();
                    needPushOut = t.getPicRedAndWhiteRodDiseaseResult().getNeedPushOut();
                    subType = t.getPicRedAndWhiteRodDiseaseResult().getDiseaseType().toString();
                    repairState = t.getPicRedAndWhiteRodDiseaseResult().getRepairState();
                    break;
                case PIC_DUSTBIN_DISEASE_RESULT:
                    bbox1 = t.getPicDustbinDiseaseResult().getBbox();
                    relBbox1 = t.getPicDustbinDiseaseResult().getRelBbox();
                    score = t.getPicDustbinDiseaseResult().getScore();
                    needPushOut = t.getPicDustbinDiseaseResult().getNeedPushOut();
                    subType = t.getPicDustbinDiseaseResult().getDiseaseType().toString();
                    repairState = t.getPicDustbinDiseaseResult().getRepairState();
                    break;
                case PIC_GARBAGE_BAG_DISEASE_RESULT:
                    bbox1 = t.getPicGarbageBagDiseaseResult().getBbox();
                    relBbox1 = t.getPicGarbageBagDiseaseResult().getRelBbox();
                    score = t.getPicGarbageBagDiseaseResult().getScore();
                    subType = t.getPicGarbageBagDiseaseResult().getDiseaseType().toString();
                    needPushOut = t.getPicGarbageBagDiseaseResult().getNeedPushOut();
                    repairState = t.getPicGarbageBagDiseaseResult().getRepairState();
                    break;
                case PIC_TRAFFIC_SIGN_DISEASE_RESULT:
                    bbox1 = t.getPicTrafficSignDiseaseResult().getBbox();
                    relBbox1 = t.getPicTrafficSignDiseaseResult().getRelBbox();
                    score = t.getPicTrafficSignDiseaseResult().getScore();
                    angle = t.getPicTrafficSignDiseaseResult().getAngle();
                    needPushOut = t.getPicTrafficSignDiseaseResult().getNeedPushOut();
                    subType = t.getPicTrafficSignDiseaseResult().getDiseaseType().toString();
                    repairState = t.getPicTrafficSignDiseaseResult().getRepairState();
                    break;
                case PIC_ISOLATION_PIER_DISEASE_RESULT:
                    bbox1 = t.getPicIsolationPierDiseaseResult().getBbox();
                    relBbox1 = t.getPicIsolationPierDiseaseResult().getRelBbox();
                    score = t.getPicIsolationPierDiseaseResult().getScore();
                    needPushOut = t.getPicIsolationPierDiseaseResult().getNeedPushOut();
                    subType = t.getPicIsolationPierDiseaseResult().getDiseaseType().toString();
                    repairState = t.getPicIsolationPierDiseaseResult().getRepairState();
                    break;
                case PIC_WORKING_DISEASE_RESULT:
                    bbox1 = t.getPicWorkingDiseaseResult().getBbox();
                    relBbox1 = t.getPicWorkingDiseaseResult().getRelBbox();
                    score = t.getPicWorkingDiseaseResult().getScore();
                    subType = t.getPicWorkingDiseaseResult().getDiseaseType().toString();
                    needPushOut = t.getPicWorkingDiseaseResult().getNeedPushOut();
                    repairState = t.getPicWorkingDiseaseResult().getRepairState();
                    break;
                case PIC_ROAD_OCCUPY_DISEASE_RESULT:
                    bbox1 = t.getPicRoadOccupyDiseaseResult().getBbox();
                    relBbox1 = t.getPicRoadOccupyDiseaseResult().getRelBbox();
                    score = t.getPicRoadOccupyDiseaseResult().getScore();
                    needPushOut = t.getPicRoadOccupyDiseaseResult().getNeedPushOut();
                    repairState = t.getPicRoadOccupyDiseaseResult().getRepairState();
                    break;
                case PIC_CROWD_DISEASE_RESULT:
                    bbox1 = t.getPicCrowdDiseaseResult().getBbox();
                    relBbox1 = t.getPicCrowdDiseaseResult().getRelBbox();
                    score = t.getPicCrowdDiseaseResult().getScore();
                    headCount = t.getPicCrowdDiseaseResult().getHeadCount();
                    needPushOut = t.getPicCrowdDiseaseResult().getNeedPushOut();
                    repairState = t.getPicCrowdDiseaseResult().getRepairState();
                    break;
                case PIC_TRAFFIC_LINE_DISEASE_RESULT:
                    bbox1 = t.getPicCrowdDiseaseResult().getBbox();
                    relBbox1 = t.getPicCrowdDiseaseResult().getRelBbox();
                    score = t.getPicCrowdDiseaseResult().getScore();
                    needPushOut = t.getPicCrowdDiseaseResult().getNeedPushOut();
                    repairState = t.getPicCrowdDiseaseResult().getRepairState();
                    subType = t.getPicWorkingDiseaseResult().getDiseaseType().toString();
                    break;
            }

            Bbox bbox = null;
            RelBbox relBbox = null;
            if (bbox1 != null) {
                bbox = Bbox.builder()
                        .h(bbox1.getH())
                        .w(bbox1.getW())
                        .x(bbox1.getX())
                        .y(bbox1.getY())
                        .build();
            }
            if (relBbox1 != null) {
                relBbox = RelBbox.builder()
                        .h(relBbox1.getH())
                        .w(relBbox1.getW())
                        .x(relBbox1.getX())
                        .y(relBbox1.getY())
                        .build();
            }

            SeverityType sType = null;
            if (severityType != null) {
                sType = SeverityType.fromValue(severityType.name());
            }
            String finalType = StringUtils.isEmpty(subType) ? type : type + "_" + subType;
            DiseaseConfigInfo diseaseConfigInfo = diseaseConfig.getMap().get(finalType);
            String scene = null;
            String classification = null;
            if (ObjectUtils.isNotEmpty(diseaseConfigInfo)) {
                scene = diseaseConfigInfo.getScene();
                classification = diseaseConfigInfo.getClassification();
            }
            RoadDiseaseResult diseaseResult = RoadDiseaseResult.builder()
                    .bbox(bbox)
                    .relBbox(relBbox)
                    .diseaseType(type)
                    .width(width)
                    .length(length)
                    .area(area)
                    .angle(angle)
                    .fallDownNumber(fallDownNumber)
                    .heightDiff(heightDiff)
                    .severityType(sType)
                    .subType(subType)
                    .roadType(roadType)
                    .needPushOut(needPushOut)
                    .headCount(headCount)
                    .type(finalType)
                    .scene(scene)
                    .classification(classification)
                    .comfortRate(comfortRate)
                    .score(score)
                    .status(fromValue(repairState))
                    //把request的diseaseUid传到FrameDTO中
                    .diseaseUid(t.getDiseaseUid())
                    .build();
            diseaseResults.add(diseaseResult);
        });

        FrameMeta.RoadPerceptionResultSet perceptionResultSet = request.getPerceptionResultSet();
        List<FrameMeta.RoadPerceptionResult> perceptionResultListList = perceptionResultSet.getRoadPerceptionResultListList();
//        String segmentPicture = bytes2Base64(perceptionResultSet.getSegmentPicture().toByteArray());
        //RequestConverter.request2FrameDTO(request, )
//        byte[] segmentBytes = ImageUtil.convertRgb2Rgba(perceptionResultSet);

        List<RoadPerceptionResult> roadPerceptionResults = new ArrayList<>();
        perceptionResultListList.forEach(t -> {
            FrameMeta.RoadPerceptionResult.ResultCase resultCase = t.getResultCase();
            RoadPerceptionResult.RoadPerceptionType type = null;
            FrameMeta.Point point;
            FrameMeta.RelPoint relPoint;
            FrameMeta.Bbox bbox1;
            FrameMeta.RelBbox relBbox1;
            List<FrameMeta.Point> outlineList = null;
            switch (resultCase) {
                case PIC_FENCE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_FENCE_RESULT;
                    point = t.getPicFenceResult().getPoint();
                    bbox1 = t.getPicFenceResult().getBbox();
                    relPoint = t.getPicFenceResult().getRelPoint();
                    relBbox1 = t.getPicFenceResult().getRelBbox();
                    outlineList = t.getPicFenceResult().getOutlineList();
                    break;
                case PIC_TRAFFIC_YELLOW_LINE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_TRAFFIC_YELLOW_LINE_RESULT;
                    point = t.getPicTrafficYellowLineResult().getPoint();
                    bbox1 = t.getPicTrafficYellowLineResult().getBbox();
                    relPoint = t.getPicTrafficYellowLineResult().getRelPoint();
                    relBbox1 = t.getPicTrafficYellowLineResult().getRelBbox();
                    outlineList = t.getPicTrafficYellowLineResult().getOutlineList();
                    break;
                case PIC_TRAFFIC_SIGN_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_TRAFFIC_SIGN_RESULT;
                    point = t.getPicTrafficSignResult().getPoint();
                    bbox1 = t.getPicTrafficSignResult().getBbox();
                    relPoint = t.getPicTrafficSignResult().getRelPoint();
                    relBbox1 = t.getPicTrafficSignResult().getRelBbox();
                    outlineList = t.getPicTrafficSignResult().getOutlineList();
                    break;
                case PIC_TRAFFIC_WHITE_LINE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_TRAFFIC_WHITE_LINE_RESULT;
                    point = t.getPicTrafficWhiteLineResult().getPoint();
                    bbox1 = t.getPicTrafficWhiteLineResult().getBbox();
                    relPoint = t.getPicTrafficWhiteLineResult().getRelPoint();
                    relBbox1 = t.getPicTrafficWhiteLineResult().getRelBbox();
                    outlineList = t.getPicTrafficWhiteLineResult().getOutlineList();
                    break;
                case PIC_MANHOLE_COVER_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_MANHOLE_COVER_RESULT;
                    point = t.getPicManholeCoverResult().getPoint();
                    bbox1 = t.getPicManholeCoverResult().getBbox();
                    relPoint = t.getPicManholeCoverResult().getRelPoint();
                    relBbox1 = t.getPicManholeCoverResult().getRelBbox();
                    outlineList = t.getPicManholeCoverResult().getOutlineList();
                    break;
                case PIC_TRAFFIC_LIGHT_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_TRAFFIC_LIGHT_RESULT;
                    point = t.getPicTrafficLightResult().getPoint();
                    bbox1 = t.getPicTrafficLightResult().getBbox();
                    relPoint = t.getPicTrafficLightResult().getRelPoint();
                    relBbox1 = t.getPicTrafficLightResult().getRelBbox();
                    outlineList = t.getPicTrafficLightResult().getOutlineList();
                    break;
                case PIC_POLE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_POLE_RESULT;
                    point = t.getPicPoleResult().getPoint();
                    bbox1 = t.getPicPoleResult().getBbox();
                    relPoint = t.getPicPoleResult().getRelPoint();
                    relBbox1 = t.getPicPoleResult().getRelBbox();
                    outlineList = t.getPicPoleResult().getOutlineList();
                    break;
                case PIC_TRAFFIC_ARROW_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_TRAFFIC_ARROW_RESULT;
                    point = t.getPicTrafficArrowResult().getPoint();
                    bbox1 = t.getPicTrafficArrowResult().getBbox();
                    relPoint = t.getPicTrafficArrowResult().getRelPoint();
                    relBbox1 = t.getPicTrafficArrowResult().getRelBbox();
                    outlineList = t.getPicTrafficArrowResult().getOutlineList();
                    break;

                case PIC_ZEBRA_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_ZEBRA_RESULT;
                    point = t.getPicZebraResult().getPoint();
                    bbox1 = t.getPicZebraResult().getBbox();
                    relPoint = t.getPicZebraResult().getRelPoint();
                    relBbox1 = t.getPicZebraResult().getRelBbox();
                    outlineList = t.getPicZebraResult().getOutlineList();
                    break;

                case PIC_STOPLINE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_STOP_LINE_RESULT;
                    point = t.getPicStoplineResult().getPoint();
                    bbox1 = t.getPicStoplineResult().getBbox();
                    relPoint = t.getPicStoplineResult().getRelPoint();
                    relBbox1 = t.getPicStoplineResult().getRelBbox();
                    outlineList = t.getPicStoplineResult().getOutlineList();
                    break;

                case PIC_CURBSTONE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_CURBSTONE_RESULT;
                    point = t.getPicCurbstoneResult().getPoint();
                    bbox1 = t.getPicCurbstoneResult().getBbox();
                    relPoint = t.getPicCurbstoneResult().getRelPoint();
                    relBbox1 = t.getPicCurbstoneResult().getRelBbox();
                    outlineList = t.getPicCurbstoneResult().getOutlineList();
                    break;
                case PIC_PEDESTRIAN_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_PEDESTRIAN_RESULT;
                    point = t.getPicPedestrianResult().getPoint();
                    bbox1 = t.getPicPedestrianResult().getBbox();
                    relPoint = t.getPicPedestrianResult().getRelPoint();
                    relBbox1 = t.getPicPedestrianResult().getRelBbox();
                    break;
                case PIC_RIDER_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_RIDER_RESULT;
                    point = t.getPicRiderResult().getPoint();
                    bbox1 = t.getPicRiderResult().getBbox();
                    relPoint = t.getPicRiderResult().getRelPoint();
                    relBbox1 = t.getPicRiderResult().getRelBbox();
                    break;
                case PIC_MOTORIZED_VEHICLE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_MOTORIZED_VEHICLE_RESULT;
                    point = t.getPicMotorizedVehicleResult().getPoint();
                    bbox1 = t.getPicMotorizedVehicleResult().getBbox();
                    relPoint = t.getPicMotorizedVehicleResult().getRelPoint();
                    relBbox1 = t.getPicMotorizedVehicleResult().getRelBbox();
                    break;
                case PIC_NON_MOTORIZED_VEHICLE_RESULT:
                    type = RoadPerceptionResult.RoadPerceptionType.PIC_NON_MOTORIZED_VEHICLE_RESULT;
                    point = t.getPicNonMotorizedVehicleResult().getPoint();
                    bbox1 = t.getPicNonMotorizedVehicleResult().getBbox();
                    relPoint = t.getPicNonMotorizedVehicleResult().getRelPoint();
                    relBbox1 = t.getPicNonMotorizedVehicleResult().getRelBbox();
                    break;
                default:
                    log.debug("un support type={}", resultCase.name());
                    return;
            }

            Bbox bbox;
            RelBbox relBbox;
            if (Objects.isNull(bbox1) && Objects.isNull(relBbox1)) {
                return;
            }

            bbox = Bbox.builder()
                    .h(bbox1.getH())
                    .w(bbox1.getW())
                    .x(bbox1.getX())
                    .y(bbox1.getY())
                    .build();
            relBbox = RelBbox.builder()
                    .h(relBbox1.getH())
                    .w(relBbox1.getW())
                    .x(relBbox1.getX())
                    .y(relBbox1.getY())
                    .build();

            Point point1 = null;
            if (point != null) {
                point1 = Point.builder()
                        .x(point.getX())
                        .y(point.getY())
                        .build();
            }

            RelPoint relPoint1 = null;
            if (relPoint != null) {
                relPoint1 = RelPoint.builder()
                        .x(relPoint.getX())
                        .y(relPoint.getY())
                        .build();
            }

            List<Point> newOutlineList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(outlineList)) {
                outlineList.forEach(pt -> newOutlineList.add(Point.builder().x(pt.getX()).y(pt.getY()).build()));
            }

            RoadPerceptionResult roadPerceptionResult = RoadPerceptionResult.builder()
                    .point(point1)
                    .bbox(bbox)
                    .relPoint(relPoint1)
                    .relBbox(relBbox)
                    .outline(newOutlineList)
                    .perceptionType(type)
                    .build();
            roadPerceptionResults.add(roadPerceptionResult);
        });

        GpsResult gpsResult = GpsResult.builder()
                .latitude(request.getGpsResult().getLatitude())
                .longitude(request.getGpsResult().getLongitude())
                .speed((int) (request.getGpsResult().getSpeed() * 3.6))
                .direction((int) request.getGpsResult().getHeading())
                .altitude(request.getGpsResult().getHeightMsl() * 1000)
                .time(request.getGpsResult().getTime())
                .trip(request.getGpsResult().getTrip() / 1000)
                .startTime(request.getGpsResult().getStartTime())
                .build();
        return FrameDTO.builder()
                .carId(request.getCarId())
                .timeStamp(request.getTimeStamp())
                .sendTimeStamp(request.getSendTimeStamp())
                .sequenceId(request.getSequenceId())
                .bumpsType(request.hasPicBumpsDiseaseResult() ? request.getPicBumpsDiseaseResult().getSeverityType().toString() : null)
                .comfortRate(request.getComfortRate())
                .coverRate(request.getCoverRate())
                .imageType(request.getPictureType().getNumber())
                .gpsLocation(gpsResult)
                .roadDiseaseResult(diseaseResults)
                .roadPerceptionResult(roadPerceptionResults)
                .algoVersion(request.getVersion())
                .picture(new String(request.getPicture().toByteArray()))
                .diseasePicture(new String(request.getPictureForDisease().toByteArray()))
                .lidarPicture((new String(request.getLidarResult().getPicture().toByteArray())))
                .segmentPicture(new String(perceptionResultSet.getSegmentPicture().toByteArray()))
                .assets(frame2Assets(request))
                .build();
    }

    public static HitStatusEnum fromValue(FrameMeta.RepairStateType repairStateType) {
        HitStatusEnum statusEnum;
        switch (repairStateType) {
            case DISEASE_STATE_SENT:
                statusEnum = HitStatusEnum.DISPATCHED;
                break;
            case DISEASE_STATE_REPAIRED:
                statusEnum = HitStatusEnum.COMPLETE;
                break;
            case DISEASE_STATE_RECHECKED:
                statusEnum = HitStatusEnum.CONFIRMED;
                break;
            default:
                statusEnum = HitStatusEnum.NEW;
                break;
        }
        return statusEnum;
    }
}
